package org.jetnuke.modules.reviews;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.dao.UserDAO;
import net.jforum.entities.User;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.ReviewsDAO;
import org.jetnuke.entities.Reviews;
import org.jetnuke.entities.ReviewsComments;

public class ShowReview implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ShowReview.class);

	int id;

	public void setId(int id) {
		this.id = id;
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		List comments = DataAccessDriver.getInst().newReviewsCommentDAO()
				.selectByRID(id);
		Iterator it = comments.iterator();
		HashMap userMap = new HashMap();
		vc.put("userMap", userMap);
		UserDAO um = DataAccessDriver.getInst().newUserDAO();
		while (it.hasNext()) {
			ReviewsComments rc = (ReviewsComments) it.next();
			int userid = rc.getUSERID();
			Integer uS = new Integer(userid);
			if (!userMap.containsKey(uS)) {
				User user = um.selectById(userid);
				userMap.put(uS, user);
			}

		}
		vc.put("comments", comments);
		ReviewsDAO rd = DataAccessDriver.getInst().newReviewsDAO();
		Reviews rv = rd.retrieveByPK(id);
		rd.incHits(rv);
		vc.put("review", rv);
		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}

}
